<template>
  <div class='login-container'>
    <div class='bg_first'>
      <img src='../../assets/login/bg@3x.png' alt=''>
      <div class='bg_two'>
        <img src='../../assets/login/TUTU@3x.png' alt=''>
      </div>
    </div>
    <el-form ref='loginForm' :model='loginForm' :rules='loginRules' class='login-form'>
      <h3 class='title'>能源统计用户平台</h3>
      <el-form-item prop='username'>
        <el-input
          v-model='loginForm.username'
          type='text'
          auto-complete='off'
          placeholder='账号'
        >
          <svg-icon slot='prefix' icon-class='user' class='el-input__icon input-icon' />
        </el-input>
      </el-form-item>
      <el-form-item prop='password'>
        <el-input
          v-model='loginForm.password'
          type='password'
          auto-complete='off'
          placeholder='密码'
          @keyup.enter.native='handleLogin'
        >
          <svg-icon slot='prefix' icon-class='password' class='el-input__icon input-icon' />
        </el-input>
      </el-form-item>
      <el-form-item v-if='captchaOnOff' prop='captcha' class='capttcha'>
        <el-input
          v-model='loginForm.captcha'
          auto-complete='off'
          placeholder='验证码'
          style='width: 63%'
          @keyup.enter.native='handleLogin'
        >
          <svg-icon slot='prefix' icon-class='validCode' class='el-input__icon input-icon' />
        </el-input>
        <div class='login-code'>
          <img :src='codeUrl' class='login-code-img' @click='getCode'>
        </div>
      </el-form-item>
      <el-checkbox v-model='loginForm.rememberMe' style='margin:0px 0px 25px 0px;'>记住密码</el-checkbox>
      <el-form-item style='width:100%;'>
        <el-button
          :loading='loading'
          size='medium'
          type='primary'
          style='width:100%;'
          @click.native.prevent='handleLogin'
        >
          <span v-if='!loading'>登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div v-if='register' style='float: right;'>
          <router-link class='link-type' :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class='el-login-footer'>
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect'
import Cookies from 'js-cookie'
import { getCodeImg } from '@/api/login'

export default {
  name: 'Login',
  // eslint-disable-next-line vue/no-unused-components
  components: { LangSelect },
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: 'admin123',
        rememberMe: false,
        captcha: '',
        key: '',
      },
      codeUrl: '',
      loginRules: {
        name: [
          { required: true, trigger: 'blur', message: '请输入您的账号' },
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
        ],
        captcha: [{ required: true, trigger: 'change', message: '请输入验证码' }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true,
    },
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        // this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        // if (this.captchaOnOff) {
        this.codeUrl = res.img
        this.loginForm.key = res.key
        // }
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
            this.loading = false
          }).catch((reason) => {
            this.loading = false
            if (this.captchaOnOff) {
              this.getCode()
            }
          })
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
  },
}
</script>

<style lang='scss'>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #000000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333333;
      height: 50px;
      /*caret-color: $cursor;*/

      /*&:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }*/
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .capttcha {
    background: none;

    .el-input {
      background: rgba(0, 0, 0, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    .login-code {
      border-radius: 5px;
      overflow: hidden;
    }
  }
}
</style>

<style lang='scss' scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.placeStyle {
  width: 94px;
  height: 16px;
  font-size: 16px;
  font-family: Source Han Sans CN;
  font-weight: 500;
  color: #999999;
}

.login-container {
  min-height: 100%;
  width: 100%;
  /*background-color: $bg;*/
  overflow: hidden;
  position: relative;

  .bg_first {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;

    img {
      width: 100%;
      height: 100%;
    }

    .bg_two {
      position: absolute;
      left: 309px;
      top: 200px;
      width: 517px;
      height: 500px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .login-form {
    position: absolute;
    right: 383px;
    top: 227px;
    width: 389px;
    max-width: 100%;
    /*padding: 160px 35px 0;*/
    margin: 0 auto;
    overflow: hidden;

    .item {
      height: 50px;
      border: 1px solid #999999;
      background-color: #fff;
      border-radius: 25px;
      margin-bottom: 30px;
    }

    .item1 {
      width: 100%;
      height: 50px;
      background-color: transparent;

      .verification {
        float: left;
        width: 204px;
        height: 50px;
        border: 1px solid #999999;
        background-color: #fff;
        border-radius: 25px;
        margin-bottom: 30px;
      }

      .codeStyle {
        float: right;
        width: 116px;
        height: 50px;
        background-color: red;
        border-radius: 10px;
        overflow: hidden;
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 5px 5px 0px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;

    img {
      width: 20px;
      height: 20px;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      /*color: $light_gray;*/
      color: #2D8FFF;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>

<style rel='stylesheet/scss' lang='scss'>
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
    width: 100%;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
